package cn.edu.scau.dao;

import cn.edu.scau.entity.Manager;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
@CacheNamespace
public interface ManagerMapper {
    /**
     * 根据用户id删除用户
     * @param userid
     * @return
     */
    @Delete({
        "delete from manager",
        "where userId = #{userId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer userid);

    /**
     * 插入新用户信息
     * @param record
     * @return
     */
    @Insert({
        "insert into manager (userId, userAccount, ",
        "userTelephone, userPassword, ",
        "userPosition, userUseflag)",
        "values (#{userId,jdbcType=INTEGER}, #{userAccount,jdbcType=VARCHAR}, ",
        "#{userTelephone,jdbcType=VARCHAR}, #{userPassword,jdbcType=VARCHAR}, ",
        "#{userPosition,jdbcType=INTEGER}, #{userUseflag,jdbcType=BIT})"
    })
    @Options(useGeneratedKeys = true,keyColumn = "userId",keyProperty = "userId")
    int insert(Manager record);

    @InsertProvider(type=ManagerSqlProvider.class, method="insertSelective")
    int insertSelective(Manager record);

    /**
     * 根据用户id查询信息
     * @param userid
     * @return
     */
    @Select({
        "select",
        "userId, userAccount, userTelephone, userPassword, userPosition, userUseflag",
        "from manager",
        "where userId = #{userid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="userId", property="userId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="userAccount", property="userAccount", jdbcType=JdbcType.VARCHAR),
        @Result(column="userTelephone", property="userTelephone", jdbcType=JdbcType.VARCHAR),
        @Result(column="userPassword", property="userPassword", jdbcType=JdbcType.VARCHAR),
        @Result(column="userPosition", property="userPosition", jdbcType=JdbcType.INTEGER),
        @Result(column="userUseflag", property="userUseflag", jdbcType=JdbcType.BIT)
    })
    Manager selectByPrimaryKey(Integer userid);
    /**
     * 根据编号模糊查询
     * @param userId
     * @return 返回部门信息对象
     */
    @SelectProvider(type = ManagerSqlProvider.class,method = "findById")
    List<Manager> findById(@Param("userId") Integer userId);
    /**
     * 根据用户账号查询信息
     * @param userAccount
     * @return
     */
    @Select({
            "select",
            "userId, userAccount, userTelephone, userPassword, userPosition, userUseflag",
            "from manager",
            "where userAccount = #{userAccount}"
    })
    @Results({
            @Result(column="userId", property="userId", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="userAccount", property="userAccount", jdbcType=JdbcType.VARCHAR),
            @Result(column="userTelephone", property="userTelephone", jdbcType=JdbcType.VARCHAR),
            @Result(column="userPassword", property="userPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column="userPosition", property="userPosition", jdbcType=JdbcType.INTEGER),
            @Result(column="userUseflag", property="userUseflag", jdbcType=JdbcType.BIT)
    })
    Manager selectByUserAccount(String userAccount);

    /**
     * 登录
     * @param userAccount 用户账号
     * @param userPassword 用户登录密码
     * @return
     */
    @Select({
            "select",
            "userId, userAccount, userTelephone, userPassword, userPosition, userUseflag",
            "from manager",
            "where userAccount = #{userAccount} and userPassword = #{userPassword}"
    })
    Manager login(@Param("userAccount") String userAccount,@Param("userPassword") String userPassword);
    /**
     * 查询所有用户信息
     * @param
     * @return
     */
    @Select({
            "select",
            "userId, userAccount, userTelephone, userPassword, userPosition, userUseflag",
            "from manager",
            "where userUseflag = true"
    })
    @Results({
            @Result(column="userId", property="userId", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="userAccount", property="userAccount", jdbcType=JdbcType.VARCHAR),
            @Result(column="userTelephone", property="userTelephone", jdbcType=JdbcType.VARCHAR),
            @Result(column="userPassword", property="userPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column="userPosition", property="userPosition", jdbcType=JdbcType.INTEGER),
            @Result(column="userUseflag", property="userUseflag", jdbcType=JdbcType.BIT)
    })
    List<Manager> selectAll();

    @UpdateProvider(type=ManagerSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Manager record);

    /**
     * 根据用户账号修改用户信息
     * @param record
     * @return
     */
    @UpdateProvider(type=ManagerSqlProvider.class, method="updateByUserAccountSelective")
    int updateByUserAccountSelective(Manager record);

    @Update({
        "update manager",
        "set userAccount = #{userAccount,jdbcType=VARCHAR},",
          "userTelephone = #{userTelephone,jdbcType=VARCHAR},",
          "userPassword = #{userPassword,jdbcType=VARCHAR},",
          "userPosition = #{userPosition,jdbcType=INTEGER},",
          "userUseflag = #{userUseflag,jdbcType=BIT}",
        "where userId = #{userId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Manager record);
}